package com.wsjj.yjh;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.DateTimeBucketAssigner;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.time.Duration;
import java.time.ZoneId;

public class SinkFile {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        env.enableCheckpointing(2000, CheckpointingMode.EXACTLY_ONCE);
//        数据生成器读取数据
        DataStreamSource<String> source = env.fromSource(new DataGeneratorSource<String>(new GeneratorFunction<Long, String>() {
            @Override
            public String map(Long value) throws Exception {
                return null;
            }
        }, Long.MAX_VALUE, RateLimiterStrategy.perSecond(1000), Types.STRING), WatermarkStrategy.noWatermarks(), "data-gensjkljads");

        FileSink<String> ouput = FileSink.forRowFormat(new Path("ouput/fink.txt"), new SimpleStringEncoder<String>("UTF-8"))
                .withOutputFileConfig(OutputFileConfig.builder()
                        .withPartPrefix("atguigu-")
                        .withPartSuffix(".log")
                        .build())
                .withBucketAssigner(new DateTimeBucketAssigner<String>("yyyy-MM-dd HH", ZoneId.systemDefault()))
                .withRollingPolicy(DefaultRollingPolicy.builder().withRolloverInterval(Duration.ofMinutes(1))
                        .withMaxPartSize(new MemorySize(1024 )).build())
                .build();

//
//        FileSink.forRowFormat(new Path(""),new SimpleStringEncoder<>("UTF-8"))
//                .withOutputFileConfig(OutputFileConfig.builder()
//                        .withPartPrefix("atguigu-")
//                        .withPartSuffix(".log")
//                        .build())
//                .withBucketAssigner()
//                .build();


        source.sinkTo(ouput);
        env.execute();
    }
}
